/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elderberryni2.data;

import elderberryni2.abstracts.BaseTimestampable;
import elderberryni2.exceptions.NoSuchTimeableArrayListException;
import elderberryni2.interfaces.IDumpable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

/**
 *
 * @author warringer
 */
public class TelemetryDataSet<E extends BaseTimestampable<N> & Comparable<E> & IDumpable, N extends Number> {
    
    private HashMap<String, TimeableArrayList<E, N>> dataSet;
    
    public TelemetryDataSet() {
        dataSet = new HashMap<String, TimeableArrayList<E, N>>();
    }
    
    public void add(String uuid, E data) {
        if (dataSet.containsKey(uuid)) {
            dataSet.get(uuid).add(data);
        } else {
            TimeableArrayList<E, N> dataList = new TimeableArrayList<E, N>();
            dataList.add(data);
            dataSet.put(uuid, dataList);
        }
    }
    
    public Set<String> getParts() {
        return dataSet.keySet();
    }
    
    public void setMaxTimeDifference(N maxTimeDifference) {
        Iterator<Entry<String, TimeableArrayList<E, N>>> iterator = dataSet.entrySet().iterator();
        while (iterator.hasNext()) {
            TimeableArrayList<E, N> dataList = iterator.next().getValue();
            dataList.setMaxTimeDifference(maxTimeDifference);
        }
    }
    
    public void setMinTimeDifference(N minTimeDifference) {
        Iterator<Entry<String, TimeableArrayList<E, N>>> iterator = dataSet.entrySet().iterator();
        while (iterator.hasNext()) {
            TimeableArrayList<E, N> dataList = iterator.next().getValue();
            dataList.setMinTimeDifference(minTimeDifference);
        }
    }
    
    public TimeableArrayList<E, N> getDataList(String uuid) throws NoSuchTimeableArrayListException {
        if (dataSet.containsKey(uuid)) {
            return dataSet.get(uuid);
        } else {
            throw new NoSuchTimeableArrayListException();
        }
    }
    
    public String[] getCSV(String uuid) throws NoSuchTimeableArrayListException {
        if (dataSet.containsKey(uuid)) {
            TimeableArrayList<E, N> dataList = dataSet.get(uuid);
            ArrayList<String> csvString = new ArrayList<String>();
            for (IDumpable data : dataList) {
                if (csvString.isEmpty()) {
                    csvString.add(data.getHeader());
                }
                csvString.add(data.toCSV());
            }
            return csvString.toArray(new String[csvString.size()]);
        } else {
            throw new NoSuchTimeableArrayListException();
        }
    }
    
    public void clear() {
        dataSet.clear();
    }
}
